//Wednesday, October 28 2009
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <sstream>
#include <algorithm>

using namespace std;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};

class MazeWanderingEasy
{
public:
	int count;
	int row;
	int col;
	vector<vector<bool> > visit;
	int decisions(vector <string> maze)
	{
		count = 0;
		row = maze.size();
		col = maze[0].size();
		for(int i=0; i<row; i++)
		{
			vector<bool> temp;
			for(int j=0; j<col; j++)
				temp.push_back(false);
			visit.push_back(temp);
		}
		for(int i=0; i<row; i++)
			for(int j=0; j<col; j++)
				if(maze[i][j] == 'M')
				{
					dfs(maze, i, j);
					break;
				}
		return count;
	}
	int dfs(vector<string> maze, int x, int y)
	{
		int ret = 0;
		int cnt = 0;
		visit[x][y] = true;
		if(maze[x][y] == '*') return 1;
		for(int i=0; i<4; i++)
		{
			int xx = x + dx[i];
			int yy = y + dy[i];
			if(inbound(xx, yy) && visit[xx][yy] == false && maze[xx][yy] != 'X')
			{
				cnt ++;
				int temp = dfs(maze, xx, yy);
				if(temp == 1) ret = 1;
			}
		}
		visit[x][y] = false;
		if(ret == 1 && cnt > 1) count++;
		return ret;
	}
	bool inbound(int x, int y)
	{
		return (x>=0  && x<row && y>=0 && y<col);
	}

};
